/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;

//import actions.ActionController;
import actions.ActionController;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author mpastarini
 */
public class DaoFactory {

    private static Properties prop     = new Properties();
    private static boolean    loadProp = false;

    private DaoFactory() {}

    public static Dao getDao(String daoName) throws Exception
    {
        try {           
            return (Dao)(Class.forName(DaoFactory.getDaoClassName(daoName))).newInstance();            
        }    
        catch(InstantiationException e1) 
        {
            System.out.println("DaoFactory: Error de inicializacion " + e1.getMessage() );
            throw new Exception("DaoFactory: " + e1.getMessage());
        } 
        catch(IllegalAccessException e2) 
        {
            System.out.println("DaoFactory: Acceso ilegal " + e2.getMessage() );
            throw new Exception("DaoFactory: " + e2.getMessage());
        }        
        catch(ClassNotFoundException e3)
        {
            System.out.println("DaoFactory: No se encontro la clase " + e3.getMessage() );
            throw new Exception("DaoFactory: " + e3.getMessage());
        }
    }
    
    
    public static Dao getDao(String daoName, String db) throws Exception
    {
        try {            
            return  (Dao)Class.forName("daos.MSSQLDao"+ daoName + "Dao").getConstructor(String.class).newInstance(db);           
        }    
        catch(InstantiationException e1) 
        {
            System.out.println("DaoFactory: Error de inicializacion " + e1.getMessage() );
            throw new Exception("DaoFactory: " + e1.getMessage());
        } 
        catch(IllegalAccessException e2) 
        {
            System.out.println("DaoFactory: Acceso ilegal " + e2.getMessage() );
            throw new Exception("DaoFactory: " + e2.getMessage());
        }        
        catch(ClassNotFoundException e3)
        {
            System.out.println("DaoFactory: No se encontro la clase " + e3.getMessage() );
            throw new Exception("DaoFactory: " + e3.getMessage());
        }
    }     
    

    private static String getDaoClassName(String daoName) throws Exception
    {
        try
        {
            if(!DaoFactory.loadProp)
            {
                FileInputStream file = new FileInputStream(ActionController.PATH_SRC + "properties\\DaoFactory.properties");
                DaoFactory.prop.load(file);
                file.close();
                DaoFactory.loadProp = true;
            }

            return "daos." + DaoFactory.prop.getProperty("CurrentDaoFactory") + daoName + "Dao";
        }
        catch(FileNotFoundException e1)
        {
            throw new Exception("DaoFactory: " + e1.getMessage());
        }
        catch(IOException e2)
        {
            throw new Exception("DaoFactory: " + e2.getMessage());
        }
    }
}
